package cn.jetpiece.cloud.joss.app.modular.object.controller;

import cn.hutool.core.util.StrUtil;
import cn.jetpiece.cloud.core.util.HttpContext;
import cn.jetpiece.cloud.joss.api.object.ObjectApi;
import cn.jetpiece.cloud.joss.api.object.model.params.PutObject;
import cn.jetpiece.cloud.joss.api.object.model.result.OssSdkBaseResult;
import cn.jetpiece.cloud.joss.api.object.model.result.UploadObjectResult;
import cn.jetpiece.cloud.joss.app.core.constant.OssConstants;
import cn.jetpiece.cloud.joss.app.modular.accessor.context.AccessorContext;
import cn.jetpiece.cloud.joss.app.modular.object.enums.BasicExceptionEnum;
import cn.jetpiece.cloud.joss.app.modular.object.model.BucketInfo;
import cn.jetpiece.cloud.joss.app.modular.object.service.BucketService;
import cn.jetpiece.cloud.joss.app.modular.object.service.ObjectService;
import cn.jetpiece.cloud.model.exception.ServiceException;
import cn.jetpiece.cloud.model.response.Result;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 对象控制器
 *
 * @author XuJZ
 * @since 2020-09-05 15:41:52
 */
@Slf4j
@RestController
@AllArgsConstructor
public class ObjectController implements ObjectApi {
    private final BucketService bucketService;
    private final ObjectService objectService;

    @Override
    public OssSdkBaseResult<UploadObjectResult> putObject(PutObject putObject, MultipartFile file) {
        String objectName = HttpContext.getRequest().getRequestURI().replace("/oss/putObject/", "").replace("oss/putObject/", "");
        String bucketName;
        if (StrUtil.isNotEmpty(putObject.getBucketName())) {
            bucketName = putObject.getBucketName();
        } else {
            bucketName = AccessorContext.me().getDefaultBucketName();
        }
        BucketInfo bucketInfo = bucketService.getBucket(bucketName);
        if (bucketInfo == null || bucketInfo.getIsEnable().equals(0)) {
            throw new ServiceException(BasicExceptionEnum.BUCKET_NOT_EXISTED);
        }
        Result<UploadObjectResult> result = objectService.putObject(objectName, putObject, bucketInfo, file);
        return new OssSdkBaseResult<>(result.isSuccess(), result.getCode(), result.getMessage(), result.getResult());
    }

    @Override
    public void getObject() {
        String requestUri = HttpContext.getRequest().getRequestURI().replace("/oss/getObject/", "").replace("oss/getObject/", "");
        String bucketName = requestUri.substring(0, requestUri.indexOf(OssConstants.PATH_SEPARATE));
        String objectName = requestUri.substring(requestUri.indexOf(OssConstants.PATH_SEPARATE));
        BucketInfo bucketInfo = bucketService.getBucket(bucketName);
        if (bucketInfo == null || bucketInfo.getIsEnable().equals(0)) {
            throw new ServiceException(BasicExceptionEnum.BUCKET_NOT_EXISTED);
        }
        objectService.getObject(objectName, bucketInfo);
    }

    @Override
    public OssSdkBaseResult<String> deleteObject() {
        String requestUri = HttpContext.getRequest().getRequestURI().replace("/oss/getObject/", "");
        String bucketName = requestUri.substring(0, requestUri.indexOf(OssConstants.PATH_SEPARATE));
        String objectName = requestUri.substring(requestUri.indexOf(OssConstants.PATH_SEPARATE));
        BucketInfo bucketInfo = bucketService.getBucket(bucketName);
        if (bucketInfo == null || bucketInfo.getIsEnable().equals(0)) {
            throw new ServiceException(BasicExceptionEnum.BUCKET_NOT_EXISTED);
        }
        objectService.deleteObject(objectName, bucketInfo);
        OssSdkBaseResult<String> result = new OssSdkBaseResult<>();
        result.setSuccess(true);
        result.setCode(200);
        result.setMessage("删除成功！");
        return result;
    }

}
